package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.UserBanquetFeastReward;
import com.yanqu.road.entity.banquet.cross.CrossFeastPartyTempData;
import com.yanqu.road.entity.enums.eBanquetType;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_BANQUET_GET_ALL_REWARD,desc = "")
public class GetFeastPartyRewardQuicklyCmd extends AbstractCommand {
    private String banquetId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossBanquetProto.BanquetGetAllPopRewardReq req = CrossBanquetProto.BanquetGetAllPopRewardReq.parseFrom(packet.getBytes());
        banquetId = req.getBanquetId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CrossFeastPartyTempData feastTempData = BanquetMgr.getFeastTempData(banquetId);
        boolean over = BanquetMgr.isCrossFeastPartyOver(banquetId);
        Property reward = new Property();
        if (!over) {
            Map<Integer, List<String>> rewardInfoMap = BanquetConfigMgr.getBanquetFeastRewardMap();
            for (Integer rewardId : rewardInfoMap.keySet()) {
                if (feastTempData.getUserCount() < Integer.valueOf(rewardInfoMap.get(rewardId).get(1))) {
                    continue;
                }
                 ret = player.getModule(BanquetModule.class).checkFeastReward(banquetId, rewardId, 0);
                if (ret != 0) {
                    continue;
                }
                Property addReward = player.getModule(BanquetModule.class).addFeastReward(banquetId, rewardId, 0);
                reward.addProperty(addReward);
                ret = 0;
            }
            try {
                Map<String, Map<Integer, UserBanquetFeastReward>> banquetRewardMap = player.getModule(BanquetModule.class).getUserBanquetFeastRewardList();
                BanquetProto.BanquetPopRewardSync.Builder respMsg = BanquetPb.parseBanquetFeastRewardListResp(banquetRewardMap, eBanquetType.ServerFeastBanquet.getValue(), true);
                player.sendPacket(Protocol.U_BANQUET_FEAST_REWARD_SYNC, respMsg);
            } catch (Exception e) {
                log.error("sync banquet error",e);
            }
        }else {
            ret = GameErrorCode.E_BANQUET_IS_OVER;
        }
        CrossBanquetProto.BanquetGetAllPopRewardResp.Builder resp = CrossBanquetProto.BanquetGetAllPopRewardResp.newBuilder();
        if (reward.isNothing()) {
            ret = GameErrorCode.E_BANQUET_NO_REWARD;
            resp.setRewards("");
        }else {
            ret = 0;
            resp.setRewards(PropertyHelper.parsePropertyToString(reward));
        }
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_BANQUET_GET_ALL_REWARD, resp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
